﻿//Interface
var Interface = function (name, methods) {
    if (arguments.length != 2) {
        throw new Error("Interface constructor called with " + arguments.length + "arguments, but expected exactly 2.");
    }

    this.name = name;
    this.methods = [];
    for(var i=0, len = methods.length;i<len;i++){
        if(typeof methods[i] !== "string"){
            throw new Error("Interface constructor expects method names to be passed in as a string.");
        }
        this.methods.push(methods[i]);
    }
}

Interface.ensureImplements = function(object){
    if(arguments.length < 2){
        throw new Error("Function Interface.ensureImplements called with " + arguments.length + "arguments, but expected at least 2.");
    }

    for(var i = 1, len = arguments.length; i < len; i++){
        var _interface = arguments[i];
        if(_interface.constructor !== Interface){
            throw new Error("Function Interface.ensureImplements expects arguments two and above to be instances of Interface.");
        }
        for(var j = 0, methodCount = _interface.methods.length; j < methodCount; j++){
            var method = _interface.methods[j];
            if(!object[method] || typeof object[method] !== "function"){
                throw new Error("Function Interface.ensureImplements: object does not implement the " + _interface.name + " interface. Method " + method + " was not found.");
            }
        }
    }
}

//Inheritance
function extend(subClass, superClass) {
    var F = function () { };
    F.prototype = superClass.prototype;
    subClass.prototype = new F();
    subClass.prototype.constructor = superClass;

    subClass.superclass = superClass.prototype;
    if (superClass.prototype.constructor == Object.prototype.constructor) {
        superClass.prototype.constructor = superClass;
    }
}
//Inheritance 2
Object.prototype.extend = function (superClass) {
    this.superClass = superClass;
    this.superClass();
    this.superClass = new superClass();
}


function addEvent(elem, event, func) {
    if (window.addEventListener) {
        elem.addEventListener(event, func, false);
    }
    else if (window.attachEvent) {
        elem.attachEvent('on' + event, func);
    }
}


// CSS
function setStyle(elements, prop, val) {
    for (var i = 0, len = elements.length; i < len; i++) {
        document.getElementById(elements[i]).style[prop] = val;
    }
}


function setCSS(el, styles) {
    for (var prop in styles) {
        if (!styles.hasOwnProperty(prop)) {
            continue;
        }
        setStyle(el, prop, styles[prop]);
    }
}

